package com.ys.controller.api.finance;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.finance.TbCompanyBankInfoAccount;
import com.ys.entity.sys.YsCol;
import com.ys.service.finance.TbCompanyBankInfoAccountService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

/**
 *
 */
@Controller
@RequestMapping("/api/inter/tbCompanyBankInfoAccount")
public class ApiInterTbCompanyBankInfoAccountController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTbCompanyBankInfoAccountController.class);

    @Autowired
    private CommonApiService<TbCompanyBankInfoAccount> commonApiService;


    @Autowired
    private TbCompanyBankInfoAccountService tbCompanyBankInfoAccountService;


    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @param conditions
     * @return
     */
    public TbCompanyBankInfoAccount setSeachSql(TbCompanyBankInfoAccount cond){

        cond.addLeftJoin("TB_CompanyBankInfo E on  E.Id = A.Pid")
                .setStatus(1)
                .addLeftJoin("TS_UserInfo B on  B.UserId = E.UserId")
                .addLeftJoin("TP_AmazonShopExchangeRate C on  C.id = E.rateId")
                .addField("B.userName,B.roleName,C.code currencyName")
                .addLeftJoin("TB_CompanyInfo ci ON ci.Vid = E.CompanyInfoVid")//所属公司
                .addField("ci.Vname AS operateCompany")//
                .addField("E.Vid AS companyBankInfoVid")
                .addField("E.Vbz AS remark")
                .addField("E.CompanyInfoVid AS operateCompanyCode")
                .addField("E.NatureAccount AS natureAccount")
                .addField("E.RateId AS currencyCode")
                .addField("E.Vkhh AS openingBank")
                .addField("E.Vyhzh AS vyhzh")
                .addField("E.vyhzhmc AS vyhzhmc")
                .addField("E.Vlhh AS interbankNumber")
                .setOrderby("A.createTime DESC");

        cond = commonApiService.dataAuth(MenuId.TB_CompanyBankInfoAccount, cond);
        return  cond;
    }

    /**
     * 查询
     */
    @RequiresPermissions("tbCompanyBankInfoAccount:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(@ModelAttribute TbCompanyBankInfoAccount cond
    ){
        try {
            cond = setSeachSql(cond);
            LayuiTableVoJA setList = commonApiService.searchLay(cond);
            return setList;
        }catch (Exception e){
            logger.error("查询出错：",e.getMessage(),e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }

    /*
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    @RequiresPermissions("tbCompanyBankInfoAccount:add")
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params,String subData, Long pid, String companyBankInfoVid, String remark, BigDecimal account
    ) {
        try {
            logger.info("补账单新增传参：" + params);
            //修改
            return tbCompanyBankInfoAccountService.add(params,subData, pid, companyBankInfoVid,remark, account);
        } catch (Exception e) {
            logger.error("补账单新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/toEdit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params,String subData
    ) {
        try {
            logger.info("补账单修改传参：" + params);
            //修改
            return tbCompanyBankInfoAccountService.edit(params,subData);
        } catch (Exception e) {
            logger.error("补账单修改出错：", e.getMessage(), e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

  
    /**
     * 删除
     * @throws Exception
     */
    @PostMapping("/delete")
    @ResponseBody
    public R delete(Long id){

        try {
            return tbCompanyBankInfoAccountService.deleteTrue(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("操作失败，请联系客服");
        }
    }
    /**
     * 导出
     */
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TbCompanyBankInfoAccount cond){
        try {
            cond.setPage(0).setSize(999999);
            cond = setSeachSql(cond);
            //获取菜单字段数据
            List<YsCol> listField = new YsCol()
                    .setMenuId(MenuId.TB_CompanyBankInfoAccount)
                    .setFrontShow(1)
                    .setOrderby("A.SortNo asc")
                    .queryList();
            //获取数据
            List<Map<String, Object>> listData = cond.queryMap();

            //处理数据
            ExportExcelUtils<TbCompanyBankInfoAccount> util = new ExportExcelUtils<>(new TbCompanyBankInfoAccount());
            String toFilePath = util.exportExcel(listData, listField, "公司银行账户实时资金管理");
            UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
            return R.ok(dto.getUrl());
        }catch (Exception e){
            logger.error("导出出错：",e.getMessage(),e);
            return R.error("导出失败，请稍后重试！");
        }
    }

}
        










